package cn.itrip.order.controller;

import cn.itrip.dto.Dto;
import cn.itrip.order.service.ItripUserLinkUserService;
import cn.itrip.pojo.ItripUser;
import cn.itrip.pojo.ItripUserLinkUser;
import cn.itrip.utlis.DtoUtils;
import cn.itrip.utlis.ValidationToken;
import cn.itrip.vo.ItripUserLinkUserVO;
import cn.itrip.vo.userinfo.ItripAddUserLinkUserVO;
import cn.itrip.vo.userinfo.ItripModifyUserLinkUserVO;
import cn.itrip.vo.userinfo.ItripSearchUserLinkUserVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 常用联系人接口
 */

@Controller
@RequestMapping("/api")
public class UserLinkUserController {

    @Resource
    private ItripUserLinkUserService itripUserLinkUserService;
    @Resource
    private ValidationToken validationToken;

    /**
     * 保存旅客信息
     * @param itripAddUserLinkUserVO
     * @return
     */
    @ApiOperation(value="保存旅客信息",httpMethod = "POST", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="保存旅客信息")
    @RequestMapping(value = "/userinfo/adduserlinkuser",method = RequestMethod.POST)
    @ResponseBody
    public Dto selectUserLink(@RequestBody ItripAddUserLinkUserVO itripAddUserLinkUserVO,HttpServletRequest request){
        ItripUserLinkUser itripUserLinkUser = new ItripUserLinkUser();
        String token = request.getHeader("token");

        ItripUser user = validationToken.getCurrentUser(token);

        itripAddUserLinkUserVO.setUserId(user.getId());
        try {
            if(itripAddUserLinkUserVO.getLinkUserName()!=null&&itripAddUserLinkUserVO.getLinkUserName().length()!=0)
            {
                itripUserLinkUser.setLinkusername(itripAddUserLinkUserVO.getLinkUserName());
            }
            if(itripAddUserLinkUserVO.getLinkIdCardType()!=null)
            {
                itripUserLinkUser.setLinkidcardtype(itripAddUserLinkUserVO.getLinkIdCardType());
            }
            if(itripAddUserLinkUserVO.getLinkIdCard()!=null&&itripAddUserLinkUserVO.getLinkIdCard().length()!=0)
            {
             itripUserLinkUser.setLinkidcard(itripAddUserLinkUserVO.getLinkIdCard());
            }
            if(itripAddUserLinkUserVO.getUserId()!=null)
            {
                itripUserLinkUser.setUserid(Integer.parseInt(itripAddUserLinkUserVO.getUserId().toString()));
            }
            if(itripAddUserLinkUserVO.getLinkPhone()!=null&&itripAddUserLinkUserVO.getLinkPhone().length()!=0)
            {
                itripUserLinkUser.setLinkphone(itripAddUserLinkUserVO.getLinkPhone());
            }
            if(itripAddUserLinkUserVO.getUserId()!=null){
                itripUserLinkUser.setUserid(Integer.parseInt(itripAddUserLinkUserVO.getUserId().toString()));
                itripUserLinkUser.setCreatedby(itripAddUserLinkUserVO.getUserId());
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return DtoUtils.returnFail("参数错误","449");
        }
        try {
            itripUserLinkUserService.saveUserLink(itripUserLinkUser);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtils.returnFail("保存失败！","440");
        }
        return DtoUtils.returnSuccess();
    }

    /**
     * 查询旅客
     * @param itripSearchUserLinkUserVO
     * @return
     */
    @ApiOperation(value="查询旅客",httpMethod = "POST", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="查询旅客")
    @RequestMapping(value = "/userinfo/queryuserlinkuser",method = RequestMethod.POST)
    @ResponseBody
    public Dto queryUserLink(@RequestBody ItripSearchUserLinkUserVO itripSearchUserLinkUserVO, HttpServletRequest request){

        String token = request.getHeader("token");

        ItripUser user = validationToken.getCurrentUser(token);

        itripSearchUserLinkUserVO.setUserId(user.getId());

        ItripUserLinkUser itripUserLinkUser = new ItripUserLinkUser();

        System.out.println(itripSearchUserLinkUserVO.getLinkUserName()+"---------------------------------------------------");

        try {
            itripUserLinkUser.setLinkusername(itripSearchUserLinkUserVO.getLinkUserName());
            if(itripSearchUserLinkUserVO.getUserId()!=null)
            {
                itripUserLinkUser.setUserid(Integer.parseInt(itripSearchUserLinkUserVO.getUserId().toString()));
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return DtoUtils.returnFail("参数错误","449");

        }
        List<ItripUserLinkUser> linkUserList = itripUserLinkUserService.queryByLinkList(itripUserLinkUser);

        List<ItripUserLinkUserVO> linkUserVOS = new ArrayList<>();

        ItripUserLinkUserVO userVO = null;
        for(ItripUserLinkUser userLinkUser:linkUserList){
            userVO = new ItripUserLinkUserVO();

            userVO.setId(userLinkUser.getId());
            userVO.setLinkPhone(userLinkUser.getLinkphone());
            userVO.setLinkIdCard(userLinkUser.getLinkidcard());
            userVO.setLinkIdCardType(userLinkUser.getLinkidcardtype());
            userVO.setLinkUserName(userLinkUser.getLinkusername());
            //userVO.setUserId(Long.parseLong(itripSearchUserLinkUserVO.getUserId().toString()));
            linkUserVOS.add(userVO);
        }
       return DtoUtils.returnDataSuccess(linkUserVOS);
    }

    /**
     * 修改旅客信息
     * @param itripModifyUserLinkUserVO
     * @return
     */
    @ApiOperation(value="修改旅客信息",httpMethod = "POST", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="修改旅客信息")
    @ResponseBody
    @RequestMapping(value = "/userinfo/modifyuserlinkuser",method = RequestMethod.POST)
    public Dto modifierUserLink(ItripModifyUserLinkUserVO itripModifyUserLinkUserVO,HttpServletRequest request){
        ItripUserLinkUser itripUserLinkUser = new ItripUserLinkUser();
        String token = request.getHeader("token");
        ItripUser user = validationToken.getCurrentUser(token);
        try {
            itripUserLinkUser.setLinkusername(itripModifyUserLinkUserVO.getLinkUserName());
            itripUserLinkUser.setLinkidcardtype(itripModifyUserLinkUserVO.getLinkIdCardType());
            itripUserLinkUser.setLinkidcard(itripModifyUserLinkUserVO.getLinkIdCard());
            itripUserLinkUser.setUserid(user.getId().intValue());
            if(itripModifyUserLinkUserVO.getLinkPhone()!=null)
            itripUserLinkUser.setLinkphone(itripModifyUserLinkUserVO.getLinkPhone());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return DtoUtils.returnFail("参数错误","449");
        }

        try {
            itripUserLinkUserService.modifierUserLink(itripUserLinkUser);
        } catch (Exception e){
            e.printStackTrace();
            DtoUtils.returnFail("修改失败！","441");
        }
        return DtoUtils.returnSuccess();
    }

    /**
     * 删除旅客
     * @param ids
     * @return
     */
    @ApiOperation(value="删除旅客",httpMethod = "GET", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="删除旅客")
    @ResponseBody
    @RequestMapping(value = "/userinfo/deluserlinkuser",method = RequestMethod.GET)
    public Dto delete(@RequestParam Long ids){

        if(itripUserLinkUserService.deleteUserLink(ids))
        return DtoUtils.returnSuccess();
        return DtoUtils.returnFail("删除失败","399");
    }
}
